package com.eshore.demo.qingjia.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eshore.common.service.IYmlService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eshore.khala.core.extension.plugins.pagination.Page;
import com.eshore.khala.core.kernel.conditions.query.QueryWrapper;
import com.eshore.khala.core.kernel.metadata.IPage;
import com.eshore.khala.core.kernel.toolkit.Wrappers;
import com.eshore.khala.core.starter.common.Result;
import com.eshore.khala.core.starter.util.CriteriaUtil;
import com.eshore.khala3.common.export.ExportUtil;
import com.eshore.khala3.common.reflect.ReflectUtil;
import com.eshore.khala.core.starter.util.PageUtil;
import com.eshore.khala.core.starter.web.controller.BaseController;
import com.eshore.demo.qingjia.entity.LzyQingjiaApplyForm;
import com.eshore.demo.qingjia.entity.LzyQingjiaApplyFormCascade;
import com.eshore.demo.qingjia.service.ILzyQingjiaApplyFormService;
import com.eshore.demo.qingjia.web.vo.LzyQingjiaApplyFormVo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

import io.swagger.annotations.ApiOperation;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2022-04-29
 */
@RestController
@RequestMapping("/api/lzyQingjiaApplyForm")
public class LzyQingjiaApplyFormController extends BaseController {
    private final static Logger logger = LoggerFactory.getLogger(LzyQingjiaApplyFormController.class);
    
    @Autowired
	private IYmlService ymlService;
    
    @Autowired
    private ILzyQingjiaApplyFormService lzyQingjiaApplyFormService;

    @HystrixCommand(fallbackMethod = "saveFallback")
    @ApiOperation(value = "新增")
    @PostMapping(value = "/save")
    public ResponseEntity<Result<LzyQingjiaApplyFormCascade>> save(@RequestBody LzyQingjiaApplyFormCascade lzyQingjiaApplyForm) {
        lzyQingjiaApplyFormService.save(lzyQingjiaApplyForm);     
        return ResponseEntity.ok().body(Result.success(lzyQingjiaApplyForm));
    }

    public ResponseEntity<Result<LzyQingjiaApplyFormCascade>> saveFallback(@RequestBody LzyQingjiaApplyFormCascade lzyQingjiaApplyForm, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }

    @HystrixCommand(fallbackMethod = "listFallback")
    @ApiOperation(value = "列表查询")
    @GetMapping(value = "/list")
    public ResponseEntity<Result<List<LzyQingjiaApplyForm>>> list(LzyQingjiaApplyFormVo lzyQingjiaApplyFormVo)
            throws InterruptedException {
        QueryWrapper<LzyQingjiaApplyForm> queryWrapper = Wrappers.<LzyQingjiaApplyForm>query();

        CriteriaUtil.vo2Qw(lzyQingjiaApplyFormVo, queryWrapper);

        List<LzyQingjiaApplyForm> result = lzyQingjiaApplyFormService.list(queryWrapper);
        return ResponseEntity.ok().body(Result.success(result));
    }

    public ResponseEntity<Result<List<LzyQingjiaApplyForm>>> listFallback(LzyQingjiaApplyFormVo lzyQingjiaApplyFormVo, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }    

    @HystrixCommand(fallbackMethod = "pageFallback")
    @ApiOperation(value = "分页查询")
    @GetMapping(value = "/page")
    public ResponseEntity<Result<IPage<LzyQingjiaApplyForm>>> page(LzyQingjiaApplyFormVo lzyQingjiaApplyFormVo, Page<?> page)
            throws InterruptedException {
        QueryWrapper<LzyQingjiaApplyForm> queryWrapper = Wrappers.<LzyQingjiaApplyForm>query();
        CriteriaUtil.vo2Qw(lzyQingjiaApplyFormVo, queryWrapper);
        IPage<LzyQingjiaApplyForm> result = lzyQingjiaApplyFormService.page(new PageUtil<LzyQingjiaApplyForm>().pageInstantof(page), queryWrapper);
        return ResponseEntity.ok().body(Result.success(result));
    }
    
    public ResponseEntity<Result<IPage<LzyQingjiaApplyForm>>> pageFallback(LzyQingjiaApplyFormVo lzyQingjiaApplyFormVo, Page<?> page, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }    

    @HystrixCommand(fallbackMethod = "getFallback")
    @ApiOperation(value = "根据id查询明细")
    @GetMapping(value = "/get/{id}")
    public ResponseEntity<Result<LzyQingjiaApplyForm>> get(@PathVariable("id") Long id) {
        LzyQingjiaApplyForm result = lzyQingjiaApplyFormService.getById(id);
        return ResponseEntity.ok().body(Result.success(result));
    }

    public ResponseEntity<Result<LzyQingjiaApplyForm>> getFallback(@PathVariable("id") Long id, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }    

    @HystrixCommand(fallbackMethod = "updateFallback")
    @ApiOperation(value = "更新")
    @PutMapping(value = "/update")
    public ResponseEntity<Result<Boolean>> update(@RequestBody LzyQingjiaApplyForm lzyQingjiaApplyForm) {
        Boolean result = lzyQingjiaApplyFormService.updateById(lzyQingjiaApplyForm);
        return ResponseEntity.ok().body(Result.success(result));
    }

    public ResponseEntity<Result<Boolean>> updateFallback(@RequestBody LzyQingjiaApplyForm lzyQingjiaApplyForm, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }        
    
    @HystrixCommand(fallbackMethod = "deleteFallback")
    @ApiOperation(value = "根据id删除，若有附表，会删除附表数据", notes = "")
    @DeleteMapping(value = "/delete/{id}")
    public ResponseEntity<Result<Boolean>> delete(@PathVariable("id") Long id) {
        Boolean result = lzyQingjiaApplyFormService.removeById(id);
        return ResponseEntity.ok().body(Result.success(result));
    }
    
    public ResponseEntity<Result<Boolean>> deleteFallback(@PathVariable("id") Long id, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }

    @HystrixCommand(fallbackMethod = "deleteBatchFallback")
    @ApiOperation(value = "根据idList批量删除，若有附表，会删除附表数据。", notes = "参数如:")
    @DeleteMapping(value = "/deleteBatch")
    public ResponseEntity<Result<Boolean>> deleteBatch(List<Long> idList) {
        Boolean result = lzyQingjiaApplyFormService.removeByIds(idList);
        return ResponseEntity.ok().body(Result.success(result));
    }

    public ResponseEntity<Result<Boolean>> deleteBatchFallback(List<Long> idList, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }
    
    @HystrixCommand(fallbackMethod = "totalFallback")
    @ApiOperation(value = "total查询")
    @GetMapping(value = "/total")
    public ResponseEntity<Result<Integer>> total(LzyQingjiaApplyFormVo lzyQingjiaApplyFormVo)
            throws InterruptedException {
        QueryWrapper<LzyQingjiaApplyForm> queryWrapper = Wrappers.<LzyQingjiaApplyForm>query();
        CriteriaUtil.vo2Qw(lzyQingjiaApplyFormVo, queryWrapper);
        int total = lzyQingjiaApplyFormService.count(queryWrapper);
        return ResponseEntity.ok().body(Result.success(total));
    }
    
    public ResponseEntity<Result<Integer>> totalFallback(LzyQingjiaApplyFormVo lzyQingjiaApplyFormVo, Throwable e) {
        logger.error("已启动降级,错误信息:", e);
        return ResponseEntity.badRequest().body(Result.failed("这是降级后的服务"));
    }
    
	@HystrixCommand(fallbackMethod = "exportFallback")
	@ApiOperation(value = "导出")
	@GetMapping(value = "/export")
	public void export(LzyQingjiaApplyFormVo lzyQingjiaApplyFormVo, Page<?> page, HttpServletRequest request,
			HttpServletResponse response) throws InterruptedException {
		page.setSize(2 ^ 100).setCurrent(1);
		QueryWrapper<LzyQingjiaApplyForm> queryWrapper = Wrappers.<LzyQingjiaApplyForm>query();
		CriteriaUtil.vo2Qw(lzyQingjiaApplyFormVo, queryWrapper);
		IPage<LzyQingjiaApplyForm> result = lzyQingjiaApplyFormService.page(new PageUtil<LzyQingjiaApplyForm>().pageInstantof(page),
				queryWrapper);

		String exportPath = ymlService.getExportPath();
		String fileName = LzyQingjiaApplyForm.class.getSimpleName() + ".xlsx";
		try {
			ExportUtil.export(exportPath, fileName, ReflectUtil.getAccessibleFieldNames(LzyQingjiaApplyForm.class),
					ReflectUtil.getFieldValuesList(result.getRecords()), request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	  
	public void exportFallback(LzyQingjiaApplyFormVo lzyQingjiaApplyFormVo, Page<?> page, HttpServletRequest request,
			HttpServletResponse response,
			Throwable e) {
		logger.error("已启动降级,错误信息:", e);
	}	  
}
